package com.example.demo.config;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;

/**
 * @Author :蕉绿
 * @Date : 2023/3/7 22:11
 * @Version : V1.0
 * @Description :
 */

//同一异常处理
@ControllerAdvice
@ResponseBody
public class MyExceptionHandler {



    @ExceptionHandler(NullPointerException.class)
    public HashMap<String,Object> nullException(NullPointerException e) {
        HashMap<String,Object> ret = new HashMap<>();
        ret.put("code",-1);
        ret.put("msg","空指针异常" + e.getMessage());
        ret.put("data",null);
        return ret;
    }

    @ExceptionHandler(Exception.class)
    public HashMap<String,Object> exception(Exception e) {
        HashMap<String,Object> ret = new HashMap<>();
        ret.put("code",-1);
        ret.put("msg","异常" + e.getMessage());
        ret.put("data",null);
        return ret;
    }
}
